package com.yuanjin.activity;

import java.util.ArrayList;

import com.yuanjin.R;
import com.yuanjin.models.ParceBitmap;
import com.yuanjin.util.Yuanjin;

import android.R.array;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class PictureFlipperActivity extends Activity {
	private ViewFlipper vf;
	private float startX = 0;
	private ImageView iv1, iv2;
	private int images[] = { R.drawable.num1, R.drawable.num2, R.drawable.num3 };
	private int view[] = { R.id.vf_imageview1, R.id.vf_imageview2 };
	private int vnum = 0;
	private int inum = 0;
	private int ilength = 0;
	private ArrayList<Object> imagelist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pictureflipper);
		dataInit();
	}

	private void dataInit() {
		iv1 = (ImageView) findViewById(R.id.vf_imageview1);
		iv2 = (ImageView) findViewById(R.id.vf_imageview2);
		vf = (ViewFlipper) findViewById(R.id.viewflipper_pf);
		iv1.setImageResource(images[0]);
		iv2.setImageResource(images[1]);

		Intent intent = getIntent();
		inum = intent.getIntExtra("position", 0);
		Log.e("TEST", "---------接收到inum-------->" + inum);
		imagelist = Yuanjin.imageListCache;
		Bitmap bbBitmap = (Bitmap) imagelist.get(inum);
		iv1.setImageBitmap(bbBitmap);
		// 获取图片的有效张数
		if (imagelist.get(imagelist.size() - 1) instanceof Bitmap) {
			ilength = imagelist.size();
		} else {
			ilength = imagelist.size() - 1;
		}
	}

	private ImageView getImageView(int id) {
		ImageView iv = new ImageView(this);
		iv.setImageResource(id);
		return iv;

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			startX = event.getX();

			break;
		case MotionEvent.ACTION_UP:
			// 右滑
			if (event.getX() > startX + 20) {
				if (inum != 0) {
					if (vnum == 0) {
						vnum = 1;
					} else {
						--vnum;
					}
					if (inum == 0) {
						inum = 2;
					} else {
						--inum;
					}
					vnum = vnum % 2;
					inum = inum % imagelist.size();

					ImageView iv = (ImageView) findViewById(view[vnum]);
					iv.setImageBitmap((Bitmap) imagelist.get(inum));

					vf.setInAnimation(this, R.anim.in_leftright);
					vf.setOutAnimation(this, R.anim.out_leftright);
					vf.showNext();
				} else {
					Toast.makeText(this, "已是第一张图片", Toast.LENGTH_SHORT).show();
				}

				// vf.getDisplay()
			} else if (event.getX() < startX - 20) {// 左滑
				if (inum < ilength - 1) {
					++vnum;
					++inum;
					vnum = vnum % 2;
					inum = inum % imagelist.size();
					ImageView iv = (ImageView) findViewById(view[vnum]);
					iv.setImageBitmap((Bitmap) imagelist.get(inum));

					vf.setInAnimation(this, R.anim.in_rightleft);
					vf.setOutAnimation(this, R.anim.out_rightleft);
					vf.showPrevious();
				} else {
					Toast.makeText(this, "没有图片了", Toast.LENGTH_SHORT).show();
				}

			}
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);

	}

}
